package com.tian.library.test

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import com.tian.library.permission.OnPermissionCallback
import com.tian.library.permission.TianPermission
import com.tian.library.test.databinding.ActivityPermissionBinding
import com.tian.library.test.utils.ToastUtil

/**
 * @author tiansongtao,Email:769001510@qq.com, created on 2020/12/15 14:17
 *
 * 权限请求依赖模块测试
 */
class PermissionUtilActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivityPermissionBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_permission)
        binding.clickListener = this
    }

    override fun onClick(v: View?) {
        when (v!!) {
            binding.perCamera -> cameraRequest()
            binding.appDetail -> TianPermission.startApplicationDetails(this)
        }
    }


    private fun cameraRequest(){
        TianPermission.with(this)
            .permission(Manifest.permission.CAMERA)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableSet<String>, all: Boolean) {
                    if (all) {
                     ToastUtil.show(this@PermissionUtilActivity, "camera授权成功")
                    }
                }

                override fun onDenied(permissions: MutableSet<String>, never: Boolean) {
                    ToastUtil.show(this@PermissionUtilActivity, "camera授权失败")
                    if (never) {
                        TianPermission.startPermissionActivity(this@PermissionUtilActivity, permissions)
                    }
                }

            })
    }
}